//
//  GQHInvoiceController.m
//  Xanthium
//
//  Created by GuanQinghao on 28/04/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHInvoiceModel.h"

#pragma mark Object

#pragma mark View
#import "GQHInvoiceView.h"

#pragma mark Controller
#import "GQHInvoiceController.h"
#import "GQHSubmitSuccessController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHInvoiceController () <GQHInvoiceViewDelegate> {
    
#pragma mark Data
    NSMutableDictionary *parameters;
#pragma mark View
    /// 自定义根视图
    GQHInvoiceView *rootView;
}

@end


@implementation GQHInvoiceController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHInvoiceView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    rootView.qh_orderModel = self.qh_orderModel;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
    [parameters setObject:_qh_orderModel.qh_orderID forKey:@"waybillId"];
    
    rootView.qh_orderNumberLabel.text = [NSString stringWithFormat:@"订单编号：%@",_qh_orderModel.qh_orderNumber];
    
    if (!_qh_editable) {
        
        [self fetchInvoice];
    }
    rootView.isEdit = _qh_editable;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"发票信息";
    
    if (_qh_editable) {
        
        [self.qh_rightButton setTitle:@"确定" forState:UIControlStateNormal];
    } else {
        
        self.qh_rightButton.hidden = YES;
    }
    
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    if (!_qh_editable) {
        
        return;
    }
    
    switch (indexPath.row) {
            
        case 0: {
            
            /// 购买方名称
            [self qh_alertWithTitle:@"请输入购买方名称" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入购买方名称";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"purchaserName"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 1: {
            
            /// 纳税人识别号
            [self qh_alertWithTitle:@"请输入纳税人识别号" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入纳税人识别号";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"taxpayerIdNum"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 2: {
            
            /// 收件地址
            [self qh_alertWithTitle:@"请输入收件地址" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入收件地址";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"address"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 3: {
            
            /// 收件电话
            [self qh_alertWithTitle:@"请输入收件电话" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入收件电话";
                textField.keyboardType = UIKeyboardTypeNumberPad;
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"phoneNum"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 4: {
            
            /// 开户账号
            [self qh_alertWithTitle:@"请输入开户账号" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入开户账号";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"openAccountNum"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 5: {
            
            /// 开户行
            [self qh_alertWithTitle:@"请输入开户行" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入开户行";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"openAccountBank"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 6: {
            
            /// 应税劳务名称
            [self qh_alertWithTitle:@"请输入应税劳务名称" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入应税劳务名称";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"laborName"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 7: {
            
            /// 规格型号
            [self qh_alertWithTitle:@"请输入规格型号" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入规格型号";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"specifications"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 8: {
            
            /// 单位
            [self qh_alertWithTitle:@"请输入单位" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入单位";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 9: {
            
            /// 数量
            [self qh_alertWithTitle:@"请输入数量" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入数量";
                textField.keyboardType = UIKeyboardTypeNumberPad;
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 10: {
            
            /// 合计税价
            [self qh_alertWithTitle:@"请输入合计税价" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入合计税价";
                textField.keyboardType = UIKeyboardTypeDecimalPad;
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                
                if (inputTextField.text.length > 0 && ([inputTextField.text qh_isValidDecimal] || [inputTextField.text qh_isOnlyNumber])) {
                    
                    [parameters setObject:inputTextField.text forKey:@"fullPrice"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        case 11: {
            
            /// 备注
            [self qh_alertWithTitle:@"请输入备注" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"请输入备注";
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                if (inputTextField.text.length > 0) {
                    
                    [parameters setObject:inputTextField.text forKey:@"note"];
                    [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:indexPath.row];
                    [rootView.qh_tableView reloadData];
                }
            } completion:nil];
        }
            break;
        default:
            break;
    }
}

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    
    NSArray * array = @[
                        @"purchaserName",
                        @"taxpayerIdNum",
                        @"address",
                        @"phoneNum",
                        @"openAccountNum",
                        @"openAccountBank",
                        @"laborName",
                        @"specifications",
                        @"",
                        @"tax",
                        @"note"];
    for (int i = 0; i < array.count; i ++)
    {
        if (i != 8)
        {
            [parameters setObject:rootView.qh_contentArray[i] forKey:array[i]];
        }
    }
    
    NSString *purchaserName = [parameters objectForKey:@"purchaserName"];
    if ([self isBlankString:purchaserName]) {
        
        [self showToastWithText:@"请输入购买方名称"];
        return;
    }
    NSString *taxpayerIdNum = [parameters objectForKey:@"taxpayerIdNum"];
    if ([self isBlankString:taxpayerIdNum]) {
        
        [self showToastWithText:@"纳税人识别号"];
        return;
    }
    NSString *address = [parameters objectForKey:@"address"];
    if ([self isBlankString:address]) {
        
        [self showToastWithText:@"请输入收件地址"];
        return;
    }
    NSString *phoneNum = [parameters objectForKey:@"phoneNum"];
    if ([self isBlankString:phoneNum]) {
        
        [self showToastWithText:@"请输入收件电话"];
        return;
    }
    if (![phoneNum qh_isValidMobile]) {
        
        [self showToastWithText:@"请输入有效的收件电话"];
        return;
    }
    NSString *openAccountNum = [parameters objectForKey:@"openAccountNum"];
    if ([self isBlankString:openAccountNum]) {
        
        [self showToastWithText:@"请输入开户账号"];
        return;
    }
    if (![openAccountNum qh_isOnlyNumber]) {
        
        [self showToastWithText:@"请输入有效的开户账号"];
        return;
    }
    NSString *openAccountBank = [parameters objectForKey:@"openAccountBank"];
    if ([self isBlankString:openAccountBank]) {
        
        [self showToastWithText:@"请输入开户行"];
        return;
    }
    NSString *laborName = [parameters objectForKey:@"laborName"];
    if ([self isBlankString:laborName]) {
        
        [self showToastWithText:@"应税劳务名称"];
        return;
    }
    NSString *specifications = [parameters objectForKey:@"specifications"];
    if ([self isBlankString:specifications]) {
        
        [self showToastWithText:@"规格型号"];
        return;
    }
    NSString *fullPrice = [parameters objectForKey:@"tax"];
    if ([self isBlankString:fullPrice]) {
        
        [self showToastWithText:@"请输入合计税价"];
        return;
    }
    
    NSString *APIURLString = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"/buInvoice/1/save"];
    
    [GQHNetHelper qh_requestGET:APIURLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@---%@---%@",code,message,data);
        
        if (0 == [code integerValue]) {
            
            [self.navigationController pushViewController:[GQHSubmitSuccessController new] animated:YES];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --PrivateMethod
- (void)fetchInvoice {
    
    NSMutableDictionary *para = [NSMutableDictionary dictionary];
    [para setObject:_qh_orderModel.qh_orderID forKey:@"waybillId"];
    
    [GQHInvoiceModel qh_requestInvoiceWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, GQHInvoiceModel *invoice) {
        
        if (status) {
            
            NSMutableArray *contentArray = [NSMutableArray array];
            
            [contentArray qh_safetySetObject:invoice.qh_purchaserName atIndex:0];
            [contentArray qh_safetySetObject:invoice.qh_taxpayerNumber atIndex:1];
            [contentArray qh_safetySetObject:invoice.qh_address atIndex:2];
            [contentArray qh_safetySetObject:invoice.qh_mobile atIndex:3];
            [contentArray qh_safetySetObject:invoice.qh_account atIndex:4];
            [contentArray qh_safetySetObject:invoice.qh_bank atIndex:5];
            [contentArray qh_safetySetObject:invoice.qh_taxableServiceName atIndex:6];
            [contentArray qh_safetySetObject:invoice.qh_specifications atIndex:7];
            [contentArray qh_safetySetObject:invoice.qh_quantity atIndex:8];
            [contentArray qh_safetySetObject:invoice.qh_totalAmount atIndex:9];
            [contentArray qh_safetySetObject:invoice.qh_note atIndex:10];
            
            rootView.qh_contentArray = contentArray;
            [rootView.qh_tableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

- (BOOL)isBlankString:(NSString *)string {
    
    if (string == nil || string == NULL) {
        return YES;
    }
    
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    
    return NO;
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
